package engineer.view.entity_model;

import entity_model.Relationship;
import javafx.collections.ObservableList;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.ComboBoxTableCell;

/**
 * Created by nuno-rafael-figueiredo on 13-05-2014.
 */
public class RoleCardinalityTableCell extends ComboBoxTableCell<Relationship.Related, Relationship.Role.Cardinality> {
    private TableView<Relationship.Related> view;
    private RequestRole requestRole;

    public RoleCardinalityTableCell(RequestRole requestRole) {
        super(entity_model.Relationship.Role.Cardinality.values());
        this.requestRole = requestRole;
    }

    @Override
    public void commitEdit(Relationship.Role.Cardinality cardinality) {
        super.commitEdit(cardinality);

        requestRole.getRole((Relationship.Related) getTableRow().getItem()).setCardinality(cardinality);

        if (view == null)
            view = getTableView();

        final ObservableList<Relationship.Related> items = view.getItems();
        view.setItems(null);
        view.layout();
        view.setItems(items);

        setItem(cardinality);
    }
}
